using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.IO;namespace MyExplore{ public partial class MainFrm : Form { public MainFrm() { InitializeComponent(); } /// <summary> /// 加载驱动器 /// </summary> private void LoadDrives() { TreeNode driver = new TreeNode(); driver.Text = "D:\\"; driver.Tag = "D:\\"; this.tvExplore.Nodes.Add(driver); } //窗体加载 private void MainFrm_Load(object sender, EventArgs e) { //添加盘符 LoadDrives(); } //单击绑定文件和文件夹信息 private void tvExplore_AfterSelect(object sender, TreeViewEventArgs e) { TreeNode node = this.tvExplore.SelectedNode; this.BindInfo(node); } //将目录绑定到treeView中 private void BindInfo(TreeNode node) { #region 绑定子目录 DirectoryInfo directoryInfo = new DirectoryInfo(node.Tag.ToString()); DirectoryInfo[] dirs = directoryInfo.GetDirectories(); foreach (DirectoryInfo di in dirs) { TreeNode temp = new TreeNode(); temp.Text = di.Name; temp.Tag = di.FullName; node.Nodes.Add(temp); } #endregion #region 绑定本目录中的文件 FileInfo[] fileInfo = directoryInfo.GetFiles(); List<MyFile> files = new List<MyFile>(); foreach (FileInfo myFile in fileInfo) { MyFile file = new MyFile(); file.FileName = myFile.Name; file.FileLength = myFile.Length; file.FileType = myFile.Extension; file.FilePath = myFile.FullName; files.Add(file); } #endregion #region 绑定listView ShowFiles(files); #endregion } public void ShowFiles(List<MyFile> files) { ListViewItem item = null; lvFiles.Items.Clear(); foreach (MyFile file in files) { item = new ListViewItem(); item.Text = file.FileName; item.SubItems.Add(file.FileLength.ToString()); item.SubItems.Add(file.FileType); item.SubItems.Add(file.FilePath); this.lvFiles.Items.Add(item); } } }}
评论